package com.fs.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.core.security.config.DBConfig;
import com.fs.core.security.util.SM4MatchJS;
import com.fs.core.util.BeanUtil;
import com.fs.orm.auth.LoginUser;
import com.fs.orm.auth.SysRole;
import com.fs.orm.dao.SysUserDao;
import com.fs.orm.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
	
	private static final Logger logger = LoggerFactory.getLogger(UserDetailServiceImpl.class);

    @Autowired
    private SysUserDao sysUserDao;

	@Autowired
	private DBConfig dBConfig;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        try {
//            username = new SM4MatchJS().replaceSymbolsAndDecode(username, dBConfig.getDecKey());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        QueryWrapper qWrapper = new QueryWrapper();
        qWrapper.eq("login_account", username);
        SysUser sysUser = sysUserDao.selectOne(qWrapper);
        LoginUser loginUser = new LoginUser();
        BeanUtil.copyAttribute(sysUser, loginUser);
        loginUser.setUsername(sysUser.getLoginAccount());
        loginUser.setPassword(sysUser.getLoginPwd());
        Set<SysRole> sysRoles = new HashSet<>();
        SysRole sysRole = new SysRole();
        sysRole.setId(1L);
        sysRole.setCode("ADMIN");
        sysRole.setName("管理员");
        sysRole.setUserId(1L);
        sysRole.setCreateTime(new Date());
        sysRole.setUpdateTime(new Date());
        sysRoles.add(sysRole);
        loginUser.setSysRoles(sysRoles);
        return loginUser;
    }
     
}
